package nc.tb.pub.ui;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;

import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;
import nc.tb.dim.Entity;
import nc.tb.dim.Scene;
import nc.tb.dim.data.DimManager;
import nc.tb.pub.BusiObj;
import nc.tb.pub.IBusiObj;
import nc.tb.pub.IDimPkConst;
import nc.tb.pub.IDimType;
import nc.tb.pub.tree.TbFCTreeModel;
import nc.tb.pub.tree.TbTreeNode;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDateTime;

import nc.vo.pub.BusinessException;

public class SetOperator {
	public static ArrayList getSubTreeMembers(DimDef dd, BusiObj[] mems,BusiObj[] subroots) throws BusinessException{
		ArrayList ret = new ArrayList();
		
		if(subroots == null || subroots.length==0)
			return ret;
		
		if(dd != null && (mems == null || mems.length == 0)){
			mems = DimManager.getAllDimMembers(dd);
		}
		if(mems == null || mems.length == 0)
			return ret;
		
		TbFCTreeModel tModel = new TbFCTreeModel(dd, mems);
		for(int i=0; i<subroots.length; i++){
			if(!ret.contains(subroots[i]))
				ret.add(subroots[i]);
			TbTreeNode subNode = tModel.getNodeByVo(subroots[i]);
			if(subNode == null){
				continue;
			}
			
			Enumeration e = subNode.depthFirstEnumeration();
			while (e.hasMoreElements()) {
				TbTreeNode tn = (TbTreeNode) e.nextElement();
				DimMember dm = (DimMember)tn.getVo();
				if(!ret.contains(dm))
					ret.add(dm);
			}
		}
		return ret;
	}
	public static ArrayList interSet(ArrayList set1, ArrayList set2) throws BusinessException{
		ArrayList ret = new ArrayList();
		if(set1 == null || set1.isEmpty() || set2 == null || set2.isEmpty())
			return ret;
		for (Iterator iter = set1.iterator(); iter.hasNext();) {
			Object element = (Object) iter.next();
			if(set2.contains(element))
				ret.add(element);
		}
		return ret;
	}
}
